package com.idione.activities;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.idione.controller.ShowSignal;
import com.idione.controller.ShowSignalInfo;
import com.idione.model.ImageAdapter;
import com.idione.model.StructImg;
import com.idione.util.LogUtil;

public class Signal extends Activity {

	private GridView gridview1,gridview2,gridview3,gridview4;
	private Button btnTabHint1, btnTabHint2, btnTabHint3, btnTabHint4;
	private TabHost tabs;
	
	private ShowSignal showSignal;
	private ShowSignalInfo showSignalInfo;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		super.onCreate(savedInstanceState);
		setContentView(R.layout.signal);

		this.findViewByIdFromActivity();
		this.setDataInGridViews();
		this.configListenerGridView();
		this.configFont();

		this.configTab();

		showSignal = new ShowSignal(this);
		showSignalInfo = new ShowSignalInfo(this);
	}

	private void configTab() {
		tabs.setup();

		TabSpec spec = tabs.newTabSpec("mitab1");
		spec.setContent(R.id.tab1);
		spec.setIndicator("", getResources().getDrawable(R.drawable.tab1));
		tabs.addTab(spec);

		spec = tabs.newTabSpec("mitab2");
		spec.setContent(R.id.tab2);
		spec.setIndicator("", getResources().getDrawable(R.drawable.tab2));
		tabs.addTab(spec);

		spec=tabs.newTabSpec("mitab3");
		spec.setContent(R.id.tab3);
		spec.setIndicator("", getResources().getDrawable(R.drawable.tab3));
		tabs.addTab(spec);

		spec=tabs.newTabSpec("mitab4");
		spec.setContent(R.id.tab4);
		spec.setIndicator("", getResources().getDrawable(R.drawable.tab4));
		tabs.addTab(spec);

		tabs.setCurrentTab(0);		
	}

	private void setDataInGridViews() {
		gridview1.setAdapter(new ImageAdapter(this, ImageAdapter.SIGN_REGULATORY));
		gridview2.setAdapter(new ImageAdapter(this, ImageAdapter.SIGN_WARNING));
		gridview3.setAdapter(new ImageAdapter(this, ImageAdapter.SIGN_INFORMATIVE));
		gridview4.setAdapter(new ImageAdapter(this, ImageAdapter.SIGN_TRANSIENT));
	}

	private void findViewByIdFromActivity() {
		gridview1 = (GridView) findViewById(R.id.GridView1);
		gridview2 = (GridView) findViewById(R.id.GridView2);
		gridview3 = (GridView) findViewById(R.id.GridView3);
		gridview4 = (GridView) findViewById(R.id.GridView4);
		
		tabs = (TabHost)findViewById(android.R.id.tabhost);

		btnTabHint1 = (Button) findViewById(R.id.BtnTabHint1);
		btnTabHint2 = (Button) findViewById(R.id.BtnTabHint2);
		btnTabHint3 = (Button) findViewById(R.id.BtnTabHint3);
		btnTabHint4 = (Button) findViewById(R.id.BtnTabHint4);
	}

	private void configListenerGridView() {
		gridview1.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				actionEventClickImage(parent, position);
			}
		});
		gridview2.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				actionEventClickImage(parent, position);
			}
		});
		gridview3.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				actionEventClickImage(parent, position);
			}
		});
		gridview4.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				actionEventClickImage(parent, position);
			}
		});

		btnTabHint1.setOnClickListener(new OnClickListener() {
			public void onClick(View call) {showSignalInfo.show(R.string.tabHint1, R.string.lblGridView1);}
		});
		
		btnTabHint2.setOnClickListener(new OnClickListener() {
			public void onClick(View call) {showSignalInfo.show(R.string.tabHint2, R.string.lblGridView2);}
		});
		
		btnTabHint3.setOnClickListener(new OnClickListener() {
			public void onClick(View call) {showSignalInfo.show(R.string.tabHint3, R.string.lblGridView3);}
		});
		
		btnTabHint4.setOnClickListener(new OnClickListener() {
			public void onClick(View call) {showSignalInfo.show(R.string.tabHint4, R.string.lblGridView4);}
		});
		
	}
	
	private void actionEventClickImage(AdapterView<?> parent, int position) {
		String name = ((StructImg)parent.getItemAtPosition(position)).getName();
		String argument = ((StructImg)parent.getItemAtPosition(position)).getExplanation();
		showSignal.show(name, 
				((StructImg)parent.getItemAtPosition(position)).getIdDrawable(), 
				argument);		
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
	}
	
	private void configFont() {
		Typeface tfb = LogUtil.getTypeFaceButton(this);
		Typeface tft = LogUtil.getTypeFaceTitle(this);

		final Button btnTabHint1 = (Button) findViewById(R.id.BtnTabHint1);
		final Button btnTabHint2 = (Button) findViewById(R.id.BtnTabHint2);
		final Button btnTabHint3 = (Button) findViewById(R.id.BtnTabHint3);
		final Button btnTabHint4 = (Button) findViewById(R.id.BtnTabHint4);
		final TextView lblTab1 = (TextView) findViewById(R.id.lblTab1);
		final TextView lblTab2 = (TextView) findViewById(R.id.lblTab2);
		final TextView lblTab3 = (TextView) findViewById(R.id.lblTab3);
		final TextView lblTab4 = (TextView) findViewById(R.id.lblTab4);

		btnTabHint1.setTypeface(tfb);
		btnTabHint2.setTypeface(tfb);
		btnTabHint3.setTypeface(tfb);
		btnTabHint4.setTypeface(tfb);
		lblTab1.setTypeface(tft);
		lblTab2.setTypeface(tft);
		lblTab3.setTypeface(tft);
		lblTab4.setTypeface(tft);
	}
}
